/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.jmmarca.util;

import java.text.Normalizer;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.text.MaskFormatter;


/**
 *
 * @author administrador
 */
public class UtilString extends UtilsString {

    /**
     * Numero a ser formatado e quantidade de casas que será preenchido
     *
     * @param numero
     * @param qtdCasas
     * @return String
     */
    public static String zerofill(int numero, int qtdCasas) {
        try {
            return String.format("%0" + qtdCasas + "d", numero);
        } catch (Exception ex) {
            return null;
        }
    }

    /*
     * Direção 1 - Esquerda | 2 - Direita
     */
    public static String stringFill(String txt, String str, int tamanho, int direcao) {
        if (txt == null || txt.trim() == "") {
            txt = "";
        }
        while (txt.contains("  ")) {
            txt = txt.replaceAll("  ", " ").trim();
        }
        txt = txt.replaceAll("[./-]", "");
        StringBuffer sb = new StringBuffer(txt);
        if (direcao == 1) { //a Esquerda
            for (int i = sb.length(); i < tamanho; i++) {
                sb.insert(0, str);
            }
        } else if (direcao == 2) {//a Direita
            for (int i = sb.length(); i < tamanho; i++) {
                sb.append(str);
            }
        }
        return sb.toString();
    }

    public static String firstUpper(String str) {
        if (str != null) {
            if (!str.equals("") && str.length() > 1) {
                return Character.toUpperCase(str.charAt(0)) + str.substring(1);
            }
        }
        return null;
    }

    public static String reverseDelimited(String string, char delimited) {
        return UtilsString.reverseDelimited(string, delimited);
    }

    public static String repetir(String string, int repeat) {
        return UtilsString.repeat(string, repeat);
    }

    public static String firstLower(String str) {
        if (str != null) {
            if (!str.equals("") && str.length() > 1) {
                return Character.toLowerCase(str.charAt(0)) + str.substring(1);
            }
        }
        return null;
    }

    public static String normalizeSpace(String string) {
        return UtilsString.normalizeSpace(string);
    }

    public static String stripTags(String var) {
        if (var != null) {
            Pattern p = Pattern.compile("</?\\p{Alpha}+ *( +\\p{Alpha}+ *=(\"(\\\\.|[\\x00-\\x21\\x23-\\x5b\\u005d-\\uffff])*\"|'(\\\\.|[\\x00-\\x26\\x28-\\x5b\\u005d-\\uffff])*'|[\\x00-\\x3d\\u003f-\\uffff]*) *)*>");
            return p.matcher(var.toString()).replaceAll("");
        }
        return null;
    }

    public static int countWords(String str) {
        if (str != null) {
            return !str.trim().equals("") ? UtilsString.countMatches(str.trim(), " ") + 1 : 0;
        }
        return 0;
    }

    public static String[] dividir(String string, String separatorString) {
        return UtilsString.split(string, separatorString);
    }

    public static boolean contains(CharSequence vC, CharSequence qC) {

        if (vC == null || qC == null) {
            return false;
        }
        String value = (String) vC;
        String query = (String) qC;

        value = normalizar(value).toUpperCase();
        query = normalizar(query).toUpperCase();

        String[] queryArray = query.split("[^a-zA-Z0-9]");
        int x = queryArray.length;
        for (String q : queryArray) {
            if (value.contains(q.trim())) {
                return true;
            }
        }
        return false;
    }

    public static String normalizar(String string, boolean upperCase) {
        if (string == null) {
            return null;
        }
        if (upperCase) {
            string = string.toUpperCase();
        } else {
            string = string.toLowerCase();
        }
        return normalizar(string);
    }

    public static String capitalizar(String string) {
        return UtilsString.capitalize(string);
    }

    public static String normalizar(String string) {
        if (string == null) {
            return "";
        }
        String padrao = "\\s{2,}";
        string = string.replace("–", "-");
        string = Normalizer.normalize(string, Normalizer.Form.NFD);
        string = string.replaceAll("[^\\p{ASCII}]", "");

        Pattern regPat = Pattern.compile(padrao);
        Matcher matcher = regPat.matcher(string);
        string = matcher.replaceAll(" ").trim();
        return string;
    }

    public static String normalizarAll(String string) {
        string = string.trim();
        string = string.replaceAll("[^\\p{L}\\p{N}]", " ").replaceAll("[ ]+", "-");
        string = Normalizer.normalize(string, Normalizer.Form.NFD);
        string = string.replaceAll("[^\\p{ASCII}]", "");

        if (string.endsWith("-")) {
            string.subSequence(0, string.length() - 1);
        }
        string = string.replaceAll("[^\\p{ASCII}]", "");
        return string;
    }

    public static String formatarString(String string, String mask) throws ParseException {
        MaskFormatter mf = new MaskFormatter(mask);
        mf.setValueContainsLiteralCharacters(false);
        return mf.valueToString(string);
    }

    public static List<String> stringToList(String str) {
        List<String> result = new ArrayList<String>();
        if (str != null) {
            result = Arrays.asList(split(str, ","));
        }
        return result;
    }

    public static String listToString(List<String> strs) {
        String result = join(strs.toArray(), ",");
        return result;
    }

    public static boolean empty(String str) {
        try {
            if (str == null || str.trim().isEmpty()) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
        }
        return true;
    }
}